<?php
include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../site_specific/external_constants.php";
include "../../includes/phpmailer/class.phpmailer.php";
include "../../includes/phpmailer/class.smtp.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/date.php";
include "../../includes/text.php";
include "../../includes/files.php";
include "../../includes/tableproperties.php";
include "../../includes/shopping_cart.php";
include "../../includes/init.php";

$PAGE_CONTENT = "";

//$USER = new User();
# Establish database connection
establishDatabaseConnection();

$id = "";
$filter = "";

#Get the page id
if(@$_GET["id"]){
	
	$id = $_GET["id"];
}

#Get the filter value from url
if(@$_GET["filter"]){
	
	$filter = $_GET["filter"];
}

#get the keyword
$keyword=$_GET["v"];

#pass the keyword value to search term
$SEARCH_TERM=$keyword;

#initialize the searching item.
$searching_item="";

#initialize the searching table
$searching_table="";

#initialize the browse group field.
$browse_group_field = "";

#initialize the browse sort field
$browse_sort_field = "";

#initialize the type name
$type_name="";

#initialize the type name plural
$type_name_plural="";

#initialize the current table
$current_table = "";

$current_groups=array();

$custom_columns = array();

if($id=='adminpages'){
	$searching_item = "p_name";
	
	$searching_table = $PROJECT_DB_TABLES['pages'];
	
	$browse_group_field = "p_sort";
	
	$browse_sort_field = "p_name";

	$current_table = $PROJECT_DB_TABLES["pages"];
	
	# Index field
	$index_field = "p_id";

	# Type name
	$type_name = "Page";
	
	# Plural
	$type_name_plural = "Pages";
	
	# Custom Columns
	$custom_columns = array();
	
	# Add Move column
	$custom_columns[] =array("Move", "\"<a href=\\\"javascript:launchPageMove('[p_id]');\\\">Move</a>\"");
	
	# Add Preview column
	$custom_columns[] = array("Preview", "\"<a href=\\\"javascript:launchPagePreview('[p_id]');\\\">Preview</a>\"");
	
	# Add Edit column
	$custom_columns[] =array("Content", "\"<a href=\\\"javascript:editPageContent('[p_id]');\\\">Content</a>\"");
	
	# Actions
	$custom_columns[] = generateAddEditColumn($index_field);
	
	$current_groups=array();
}
else if($id=='admingroups'){
	
	$searching_item = "g_name";
	
	$searching_table = $PROJECT_DB_TABLES['groups'];
	
	$browse_group_field = "";
	
	$browse_sort_field = "g_name";
	
	$type_name_plural = "Groups";
	
	$type_name = "Group";
	
	$current_table = $PROJECT_DB_TABLES["groups"];
	
	# Index field
	$index_field = "g_id";
	
	# Custom Columns
	$custom_columns = array();
	
	# Add custom column for editing users in this group
	$custom_columns[] =array("Users", "\"<a href=\\\"javascript:editGroupUsers('[g_id]');\\\">Edit Users</a>\"");
	
	# Actions
	$custom_columns[] = generateAddEditColumn($index_field);
}
else if($id=='adminusers'){
	
	$searching_item="u_fname";
	
	$searching_table = $PROJECT_DB_TABLES['users'];
	
	$browse_group_field = "";//g_name
	
	$browse_sort_field = "u_id";
	
	$current_table = $PROJECT_DB_TABLES["users"];
	
	# Index field
	$index_field = "u_id";

	# Type name
	$type_name = "User";

	# Plural
	$type_name_plural = "Users";

	# Custom Columns
	$custom_columns = array();

	# Change password
	$custom_columns[] = array("Password", "\"<a href=\\\"javascript:openChangePasswordAdminWin('[u_id]');\\\">Change Password</a>\"");

	# Actions
	$custom_columns[] = generateAddEditColumn($index_field);
	
	$current_groups_sql = "SELECT g_id, g_name FROM gl_groups ORDER BY g_name ASC";

	$current_groups = getRecord($current_groups_sql);

}
else if($id=='adminproperties'){
	
	$searching_item="tp_name";
	
	$searching_table = $PROJECT_DB_TABLES['table_properties'];
	
	$browse_group_field = "tp_table";
	
	$browse_sort_field = "tp_formorder";
	
	$type_name_plural = "Properties";
	
	$type_name = "Property";
	
	$current_table = $PROJECT_DB_TABLES["table_properties"];
	
	# Index field
	$index_field = "tp_id";
	
	# Custom Columns
	$custom_columns = array();
	
	# Actions
	$custom_columns[] = generateAddEditColumn($index_field);
}


$data_formatting = getRecord("SELECT * FROM " . $PROJECT_DB_TABLES["table_properties"] . " WHERE tp_table = '" . $current_table . "' ORDER BY tp_browsesortorder ASC");

# Create TableProperties
$data_properties = new TableProperties();

foreach ($data_formatting as $row_format) {

	# If removing some, check them now
	if (@!is_array($remove_columns) || findInArray($remove_columns, $row_format["tp_field"]) === false || ($PAGE_MODE != "" && $PAGE_MODE != "delete")) {
		$data_properties -> addProperty(new TableProperty($row_format["tp_field"], $row_format));
	}
}

# Add custom columns
foreach ($custom_columns as $custom_column) {
	$data_properties -> addCustomColumn($custom_column[0], $custom_column[1]);
}


$sql_dropdownlist="SELECT * FROM ".$searching_table." WHERE ".$searching_item." LIKE '%".$keyword."%' order by " .$browse_sort_field . " ASC";

$data_dropdownlist=getRecord($sql_dropdownlist);

//print_r($data_dropdownlist);

$response="";

$name_class = preg_replace(
	array("/\ /i"),
	array(""),
	strtolower($type_name));

if(!(($id=='adminpages' && $SEARCH_TERM=='') || ($id=='adminusers' && $SEARCH_TERM==''))){
	$response.=$data_properties -> drawSearchingTable($data_dropdownlist, (($SEARCH_TERM == "") ? "Browsing " : "Search results in ") . $type_name_plural, $browse_group_field, $name_class);
}
else if($id=='adminpages' && $SEARCH_TERM==''){
	//echo $USER -> group;
	
	echo genertatePageForm($USER -> group_name);
}
else if($id=='adminusers' && $SEARCH_TERM==''){
			
	$sql_adminuser="SELECT gl_users.*, gl_groups.g_name, gl_groups.g_id FROM gl_users, gl_groups WHERE gl_users.u_group = gl_groups.g_id AND (u_group = 2 OR u_group = 3 OR u_group = 8 OR u_group = 9 OR u_group = 1 OR u_group = 7 OR u_group = 6 OR u_group = 10 OR u_group = 4 OR u_group = 5 OR u_group = 6 OR u_group = 10 OR u_group = 11 OR u_group = 12 OR u_group = 13 OR u_group = 14 OR u_group = 15 OR u_group = 16 OR u_group = 17 OR u_group = 18 OR u_group = 19 OR u_group = 20 OR u_group = 21 OR u_group = 22 OR u_group = 23) ORDER BY g_name ASC, u_id ASC"
;

	$current_data=getRecord($sql_adminuser);
	
	addContent("<table class=\"admintable\" cellspacing=\"0\">" .
		"<thead><tr><th>Type</th><th>User ID</th><th>User's Login Name</th><th>Date Created</th><th>Last Log In</th><th>Password</th><th>Actions</th></tr></thead>" .
		"<tbody>");

	addContent("<tr class=\"rowsubheader\"><td colspan=\"7\"><strong>Developer</strong></td></tr>");

	adminUserPrintOut($current_data, $current_groups,"Developers");

	addContent("<tr class=\"rowsubheader\"><td colspan=\"7\"><strong>Administrator</strong></td></tr>");

	adminUserPrintOut($current_data, $current_groups,"Administrators");
	
	addContent("<tr class=\"rowsubheader\"><td colspan=\"7\"><strong>Others</strong></td></tr>");

	adminUserPrintOut($current_data, $current_groups,"Others");

	addContent("</tbody></table>");	
	
	$response.=$PAGE_CONTENT;

}

echo $response;

function adminPageFilterJunkPrintOut($current_data){
	global $response;
	foreach ($current_data as $current_page_id => $current_page) {

		if ($current_page["p_status"]==3) {
	
				 $response.="<tr>" .
					"<td>".$current_page["p_id"]."</td>" .
					"<td>" . getPageStatusName($current_page["p_status"]) . "</td>" .
					"<td><a href=\"javascript:doAction('restore', '" . $current_page_id . "', false);\">Restore</a></td></tr>";		
		}
	}
}

function adminPageFilterPrintOut($current_pages = array(), $parent_id = "", $section = "site", $depth = 0) {
	global $response;
	global $ROW_COUNT;
	if (!$ROW_COUNT) {
		$ROW_COUNT = 0;
	}

	foreach ($current_pages as $current_page_id => $current_page) {

		if ($current_page["p_parent"] == $parent_id && $current_page["p_section"] == $section && $current_page["p_status"]<3) {
				$padding = "";
				for ($i = 0; $i < $depth; $i++) {
					$padding .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
	
				 $response.="<tr" . ($ROW_COUNT % 2 == 0 ? " class=\"alternate\"" : "") . ">" .
					"<td>" . $padding . "<a href=\"../" . $current_page["p_section"] . "/" . $current_page["p_id"] . ".php\">" . $current_page["p_name"] . "</a> (" . $current_page["p_id"] . ")</td>" .
					"<td>" . getPageStatusName($current_page["p_status"]) . "</td>" .
					"<td><a href=\"javascript:launchPageMove('" . $current_page["p_id"] . "');\">Move</a></td>" .
					"<td><a href=\"javascript:launchPagePreview('" . $current_page["p_id"] . "');\">Preview</a></td>" .
					"<td><a href=\"javascript:editPageContent('" . $current_page["p_id"] . "');\">Content</a></td>" .
					"<td><a href=\"javascript:doAction('edit', '" . $current_page["p_id"] . "', false);\">Edit</a> / <a href=\"javascript:doAction('remove', '" . $current_page["p_id"] . "', true);\">Remove</a></td></tr>";
	
				$ROW_COUNT++;
	
				#addContent("<p>" . $depth . ": Page: " . $current_page_id . " (Parent: " . $current_page["p_parent"] . ")</p>");
	
				adminPageFilterPrintOut($current_pages, $current_page["p_id"], $section, $depth + 1);
		}
	}
}



?>